﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hr.IDAL;
using Hr.Model;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Common.CommandTrees;
using Microsoft.EntityFrameworkCore.Internal;

namespace Hr.DAL
{
    public partial class ArticleDAL : BaseDAL<Article_List>, IArticle_List
    {
        //创建EF对象
        private DbContext db = DbContextFactory.Create();

        //DAL方法,核心

        #region 新闻管理页面数据

        /// <summary>
        /// 新闻管理页面数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新闻管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>
        public IEnumerable<object> ArticleList()
        {
            return (from ArticleList in db.Set<Article_List>()
                    join UserList in db.Set<User_List>() on ArticleList.CreateUser equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ArticleList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ArticleList.StatusID equals SysStatusID.ID
                    where SysTypeID.TableName.Equals("Article_List") && SysStatusID.TableName.Equals("Article_List")
                    select new
                    {
                        ArticleList.pk_Article,
                        SysTypeID.TypeName_CHS,
                        ArticleList.Title,
                        ArticleList.Clicks,
                        UserList.FullName,
                        ArticleList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual
                    }).ToList();
        }
        #endregion

        #region 根据Title查询新闻管理页面数据

        /// <summary>
        /// 根据Title查询新闻管理页面数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新闻管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>
        public IEnumerable<object> TitleSelectArticleList(string Title)
        {
            return (from ArticleList in db.Set<Article_List>()
                    join UserList in db.Set<User_List>() on ArticleList.CreateUser equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ArticleList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ArticleList.StatusID equals SysStatusID.ID
                    where SysTypeID.TableName.Equals("Article_List") && SysStatusID.TableName.Equals("Article_List") && ArticleList.Title.Contains(Title)
                    select new
                    {
                        ArticleList.pk_Article,
                        SysTypeID.TypeName_CHS,
                        ArticleList.Title,
                        ArticleList.Clicks,
                        UserList.FullName,
                        ArticleList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual
                    }).ToList();
        }
        #endregion


        #region 根据pk_Article删除新闻管理页面数据

        /// <summary>
        /// 根据pk_Article删除新闻管理页面数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新闻管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>
        public void DelArticleList(int pk_Article)
        {
            var data = (from ArticleList in db.Set<Article_List>()
                        where ArticleList.pk_Article == pk_Article
                        select ArticleList).FirstOrDefault();

            db.Set<Article_List>().Remove(data);

        }

        #endregion


        #region 类型和状态下拉框
        /// <summary>
        /// 下拉框绑定
        /// </summary>
        /// <param name="id">类型和状态下拉框</param>
        /// <returns>修改和增加页面里的下拉框</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>

        public IEnumerable<Sys_TypeID> SelectTypeBinding()
        {
            return db.Set<Sys_TypeID>().Where(n => n.TableName == "Article_List").ToList();
        }
        public IEnumerable<Sys_StatusID> SelectStatusBinding()
        {
            return db.Set<Sys_StatusID>().Where(n => n.TableName == "Article_List").ToList();
        }
        #endregion

        #region 新闻查看 包括记录点击数
        /// <summary>
        /// 新闻查看 包括记录点击数
        /// </summary>
        /// <param name="id">pk_Article</param>
        /// <returns>新闻查看</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/24</create>
        /// <updata>null</updata>
        public object ArticleDataBinding(int pk_Article)
        {
            Article_List article = db.Set<Article_List>().SingleOrDefault(n => n.pk_Article == pk_Article);

            article.Clicks += 1;

            db.Set<Article_List>().AddOrUpdate(article);

            db.SaveChanges();

            return (from ArticleList in db.Set<Article_List>()
                    join SysTypeID in db.Set<Sys_TypeID>() on ArticleList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ArticleList.StatusID equals SysStatusID.ID
                    where SysTypeID.TableName.Equals("Article_List") && SysStatusID.TableName.Equals("Article_List") && ArticleList.pk_Article == pk_Article
                    select new
                    {
                        ArticleList.pk_Article,
                        SysTypeID.TypeName_CHS,
                        ArticleList.Title,
                        ArticleList.Source,
                        ArticleList.Clicks,
                        ArticleList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ArticleList.Description
                    }).FirstOrDefault();
        }
        #endregion

        #region 新闻管理修改添加
        /// <summary>
        /// 新闻管理修改添加
        /// </summary>
        /// <param name="id">pk_Article</param>
        /// <returns>修改和增加页面</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>

        public Article_List GetModels(int pk_Article)
        {
            return db.Set<Article_List>().Where(n => n.pk_Article == pk_Article).FirstOrDefault();
        }

        public void EditArticleList(Article_List al, string type, int CreateUser)
        {
            if (type.Equals("Add"))
            {
                //副标题
                al.Subtitle = "";
                //视觉
                al.Visual = "";
                //来源Url
                al.SourceUrl = "";
                //图片路径
                al.ImagePath = "";
                //问题
                al.Issue = true;
                //是否评论
                al.IsComment = false;
                //是否推荐
                al.IsRecommend = false;
                //成绩
                al.Score = 0;
                al.ScoreIn = 0;
                //开始时间
                al.StartDate = DateTime.Now;
                //结束时间
                al.EndDate = DateTime.Now;
                //创建用户
                al.CreateUser = CreateUser;
                //创建时间
                al.CreateDate = DateTime.Now;
                //最后修改用户
                al.ModifyUser = CreateUser;
                //最后修改时间
                al.ModifyDate = DateTime.Now;
                //点击数
                al.Clicks = 0;

                db.Set<Article_List>().Add(al);

            }
            else if (type.Equals("Updata"))
            {
                Article_List article = db.Set<Article_List>().SingleOrDefault(n => n.pk_Article == al.pk_Article);

                article.TypeID = al.TypeID;
                article.StatusID = al.StatusID;
                article.Title = al.Title;
                article.Tags = al.Tags;
                article.Source = al.Source;
                article.Author = al.Author;
                article.SortID = al.SortID;
                article.RolePower = al.RolePower;
                article.Description = al.Description;
                //最后修改用户
                article.ModifyUser = CreateUser;
                //最后修改时间
                article.ModifyDate = DateTime.Now;

                db.Set<Article_List>().AddOrUpdate(article);
            }
        }
        #endregion



        //实现接口
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
